package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.ram.model.v20150501.DeleteAccessKeyRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.servicebase.exception.EduException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantPropertiesUtil;
import com.atguigu.vod.utils.InitVodClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Autowired
    private ConstantPropertiesUtil constantPropertiesUtil;

    /**
     * 流式上传接口 上传视频
     *
     * @param file
     * @return
     */
    @Override
    public String uploadAliYunVideo(MultipartFile file) {
        String accessKeyId = constantPropertiesUtil.getAccessKeyId();
        String accessKeySecret = constantPropertiesUtil.getAccessKeySecret();
        String fileName = file.getOriginalFilename();// 上传文件原始名字
        // 01.mp4 截取.后面的
        String title = fileName.substring(0, fileName.lastIndexOf("."));// 视频显示的名字
        try {
            InputStream inputStream = file.getInputStream();// 上传文件输入流
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            // 返回视频的Id
            return response.getVideoId();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据视频Id删除阿里云视频
     *
     * @param videoId
     */
    @Override
    public void removeAliYunVideo(String videoId) {
        // 初始化对象
        try {
            DefaultAcsClient initVodClient = InitVodClient.initVodClient(constantPropertiesUtil.getAccessKeyId(), constantPropertiesUtil.getAccessKeySecret());
            // 创建删除的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            // 设置Id
            deleteVideoRequest.setVideoIds(videoId);
            initVodClient.getAcsResponse(deleteVideoRequest);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new EduException("删除失败", 200001);
        }

    }

    /**
     * 批量删除阿里云视频
     *
     * @param videoIdList
     */
    @Override
    public void removeBatchAliYunVideos(List videoIdList) {
        // 初始化对象
        try {
            DefaultAcsClient initVodClient = InitVodClient.initVodClient(constantPropertiesUtil.getAccessKeyId(), constantPropertiesUtil.getAccessKeySecret());
            // 创建删除的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            // 将videoIdList转换成 1,2,3
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");
            // 设置Id
            deleteVideoRequest.setVideoIds(videoIds);
            initVodClient.getAcsResponse(deleteVideoRequest);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new EduException("删除失败", 200001);
        }
    }

    /**
     * 根据视频Id获取视频播放凭证
     *
     * @param videoId
     */
    @Override
    public String getPlayAuth(String videoId) {
        // 创建初始化对象
        DefaultAcsClient initVodClient = null;
        try {
            initVodClient = InitVodClient.initVodClient(constantPropertiesUtil.getAccessKeyId(), constantPropertiesUtil.getAccessKeySecret());
            // 创建Request和Response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

            // 向request对象中存放视频Id
            request.setVideoId(videoId);
            // 调用初始化对象里面的方法 调用Request  获取数据
            GetVideoPlayAuthResponse response = initVodClient.getAcsResponse(request);
            return response.getPlayAuth();
            //播放凭证
            // System.out.print("PlayAuth = " + +"\n");
            //VideoMeta信息
            // System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
        } catch (ClientException e) {
            throw new EduException("获取凭证失败", 20001);
        }

    }
}
